home *** CD-ROM | disk | FTP | other *** search
/ Linux 68k 2000 / Linux 68k 2000.iso / dokus / Serial-Programming-HOWTO < prev    next >
Encoding:
Text File  |  1997-07-06  |  23.2 KB  |  582 lines

  1.   The Linux Serial Programming HOWTO
  2.   by Peter H. Baumann, Peter.Baumann@dlr.de
  3.   v0.3, 14 June 1997
  4.  
  5.   This document describes how to program communications with devices
  6.   over a serial port on a Linux box.
  7.  
  8.   1.  Introduction
  9.  
  10.   This is the Linux Serial Programming HOWTO.  All about how to program
  11.   communications with other devices / computers over a serial line under
  12.   Linux. Different techniques are explained: Canonical I/O (only
  13.   complete lines are transmitted/received), asyncronous I/O, and waiting
  14.   for input from multiple sources.
  15.  
  16.   This document does not describe how to set up serial ports, because
  17.   this has been described by Greg Hankins in the Serial-HOWTO.
  18.  
  19.   I have to emphazize that I am not an expert in this field, but have
  20.   had problems with a project that involved such communication. The code
  21.   examples presented here were derived from the miniterm code available
  22.   from the LDP programmers guide
  23.   (ftp://sunsite.unc.edu/pub/Linux/docs/LDP/programmers-
  24.   guide/lpg-0.4.tar.gz and mirrors) in the examples directory. If
  25.   anybody has any comments, I will glady incorporate them into this
  26.   document (see sect. Feedback).
  27.  
  28.   All examples were tested using a i386 Linux Kernel 2.0.29.
  29.  
  30.   1.1.  Copyright
  31.  
  32.   The Linux Serial-Programming-HOWTO is copyright (C) 1997 by Peter
  33.   Baumann.  Linux HOWTO documents may be reproduced and distributed in
  34.   whole or in part, in any medium physical or electronic, as long as
  35.   this copyright notice is retained on all copies. Commercial
  36.   redistribution is allowed and encouraged; however, the author would
  37.   like to be notified of any such distributions.
  38.  
  39.   All translations, derivative works, or aggregate works incorporating
  40.   any Linux HOWTO documents must be covered under this copyright notice.
  41.   That is, you may not produce a derivative work from a HOWTO and impose
  42.   additional restrictions on its distribution. Exceptions to these rules
  43.   may be granted under certain conditions; please contact the Linux
  44.   HOWTO coordinator at the address given below.
  45.  
  46.   In short, we wish to promote dissemination of this information through
  47.   as many channels as possible. However, we do wish to retain copyright
  48.   on the HOWTO documents, and would like to be notified of any plans to
  49.   redistribute the HOWTOs.
  50.  
  51.   If you have questions, please contact Greg Hankins, the Linux HOWTO
  52.   coordinator, at gregh@sunsite.unc.edu via email.
  53.  
  54.   1.2.  New Versions Of This Document
  55.  
  56.   New versions of the Serial-Programming-HOWTO will be available at
  57.   and mirror sites.  There are other formats, such as PostScript and DVI
  58.   versions in the other-formats directory.  The Serial-Programming-HOWTO
  59.   is also available at http://sunsite.unc.edu/LDP/HOWTO/Serial-
  60.   Programming-HOWTO.html and will be posted to comp.os.linux.answers
  61.   monthly.
  62.  
  63.   1.3.  Feedback
  64.  
  65.   Please send me any corrections, questions, comments, suggestions, or
  66.   additional material. I would like to improve this HOWTO!  Tell me
  67.   exactly what you don't understand, or what could be clearer.  You can
  68.   reach me at Peter.Baumann@dlr.de via email. Please include the version
  69.   number of the Serial-Programming-HOWTO when writing, this is version
  70.   0.3.
  71.  
  72.   2.  Getting started
  73.  
  74.   2.1.  Debugging
  75.  
  76.   The best way to debug your code is to set up another Linux box, and
  77.   connect the two computers via a null-modem cable. Use miniterm
  78.   (available from the LDP programmers guide
  79.   (ftp://sunsite.unc.edu/pub/Linux/docs/LDP/programmers-
  80.   guide/lpg-0.4.tar.gz in the examples directory) to transmit characters
  81.   to your Linux box. Miniterm can be compiled very easily and will
  82.   transmit all keyboard input raw over the serial port. Only the define
  83.   statement #define MODEMDEVICE "/dev/ttyS0" has to be checked. Set it
  84.   to ttyS0 for COM1, ttyS1 for COM2, etc.. It is essential for testing,
  85.   that all characters are transmitted raw (without output processing)
  86.   over the line. To test your connection, start miniterm on both
  87.   computers and just type away. The characters input on one computer
  88.   should appear on the other computer and vice versa. The input will not
  89.   be echoed to the attached screen.
  90.  
  91.   To make a null-modem cable you have to cross the TxD (transmit) and
  92.   RxD (receive) lines. For a description of a cable see sect. 7 of the
  93.   Serial-HOWTO.
  94.  
  95.   It is also possible to perform this testing with only one computer, if
  96.   you have two unused serial ports. You can then run two miniterms off
  97.   two virtual consoles. If you free a serial port by disconnecting the
  98.   mouse, remember to redirect /dev/mouse if it exists. If you use a
  99.   multiport serial card, be sure to configure it correctly. I had mine
  100.   configured wrong and everything worked fine as long as I was testing
  101.   only on my computer. When I connected to another computer, the port
  102.   started loosing characters. Executing two programs on one computer
  103.   just isn't fully asynchronous.
  104.  
  105.   2.2.  Port Settings
  106.  
  107.   The devices /dev/ttyS* are intended to hook up terminals to your Linux
  108.   box, and are configured for this use after startup. This has to be
  109.   kept in mind when programming communication with a raw device. E.g.
  110.   the ports are configured to echo characters sent from the device back
  111.   to it, which normally has to be changed for data transmission.
  112.  
  113.   All parameters can be easily configured from within a program. The
  114.   configuration is stored in a structure struct termios, which is
  115.   defined in <asm/termbits.h>:
  116.  
  117.   #define NCCS 19
  118.   struct termios {
  119.           tcflag_t c_iflag;               /* input mode flags */
  120.           tcflag_t c_oflag;               /* output mode flags */
  121.           tcflag_t c_cflag;               /* control mode flags */
  122.           tcflag_t c_lflag;               /* local mode flags */
  123.           cc_t c_line;                    /* line discipline */
  124.           cc_t c_cc[NCCS];                /* control characters */
  125.   };
  126.  
  127.   This file also includes all flag definitions. The input mode flags in
  128.   c_iflag handle all input processing, which means that the characters
  129.   sent from the device can be processed before they are read with read.
  130.   Similarly c_oflag handles the output processing. c_cflag contains the
  131.   settings for the port, as the baudrate, bits per character, stop bits,
  132.   etc.. The local mode flags stored in c_lflag determine if characters
  133.   are echoed, signals are sent to your program, etc.. Finally the array
  134.   c_cc defines the control characters for end of file, stop, etc..
  135.   Default values for the control characters are defined in
  136.   <asm/termios.h>. The flags are described in the manual page
  137.   termios(3).
  138.  
  139.   The structure termios contains the c_line element. This element is
  140.   neither mentioned in the manual page for termios of Linux, nor in the
  141.   manual page of Solaris 2.5. Could somebody shed some light on this?
  142.   Shouldn't it be only included in the structure termio?
  143.  
  144.   2.3.  Input Concepts for Serial Devices
  145.  
  146.   Here three different input concepts will be presented. The appropriate
  147.   concept has to be chosen for the intended application. Whenever
  148.   possible, do not loop reading single characters to get a complete
  149.   string. When I did this, I lost characters, whereas a read for the
  150.   whole string did not show any errors.
  151.  
  152.   2.3.1.  Canonical Input Processing
  153.  
  154.   This is the normal processing mode for terminals, but can also be
  155.   useful for communicating with other devices. All input is processed in
  156.   units of lines, which means that a read will only return a full line
  157.   of input. A line is by default terminated by a NL (ASCII LF), an end
  158.   of file, or an end of line character. A CR (the DOS/Windows default
  159.   end-of-line) will not terminate a line with the default settings.
  160.  
  161.   Canonical input processing can also handle the erase, delete word, and
  162.   reprint characters, translate CR to NL, etc..
  163.  
  164.   2.3.2.  Non-Canonical Input Processing
  165.  
  166.   Non-Canonical Input Processing will handle a fixed amount of
  167.   characters per read, and allows for a character timer. This mode
  168.   should be used if your application will always read a fixed number of
  169.   characters, or if the connected device sends bursts of characters.
  170.  
  171.   2.3.3.  Asynchronous Input
  172.  
  173.   The two modes described above can be used in synchronous and
  174.   asynchronous mode. Synchronous is the default, where a read statement
  175.   will block, until the read is satisfied. In asynchronous mode the read
  176.   statement will return immedeatly and send a signal to the calling
  177.   program upon completion. This signal can be received by a signal
  178.   handler.
  179.  
  180.   2.3.4.  Waiting for Input from Multiple Sources
  181.  
  182.   This is not a different input mode, but might be useful, if you are
  183.   handling multiple devices. In my application I was handling input over
  184.   a TCP/IP socket and input over a serial connection from another
  185.   computer quasi-simultaneously. The program example given below will
  186.   wait for input from two different input sources. If input from one
  187.   source becomes available, it will be processed, and the program will
  188.   then wait for new input.
  189.  
  190.   The approach presented below seems rather complex, but it is important
  191.   to keep in mind that Linux is a multi-processing operating system. The
  192.   select system call will not load the CPU while waiting for input,
  193.   whereas looping until input becomes available would slow down other
  194.   processes executing at the same time.
  195.  
  196.   3.  Program Examples
  197.  
  198.   All examples have been derived from miniterm.c. The type ahead buffer
  199.   is limited to 255 characters, just like the maximum string length for
  200.   canonical input processing (<linux/limits.h> or <posix1_lim.h>).
  201.  
  202.   See the comments in the code for explanation of the use of the
  203.   different input modes. I hope that the code is understandable. The
  204.   example for canonical input is commented best, the other examples are
  205.   commented only where they differ from the example for canonical input
  206.   to emphasize the differences.
  207.  
  208.   The descriptions are not complete, but you are encouraged to
  209.   experiment with the examples to derive the best solution for your
  210.   application.
  211.  
  212.   Don't forget to give the appropriate serial ports the right
  213.   permissions (e. g.: chmod a+rw /dev/ttyS1)!
  214.  
  215.   3.1.  Canonical Input Processing
  216.  
  217.   #include <sys/types.h>
  218.   #include <sys/stat.h>
  219.   #include <fcntl.h>
  220.   #include <termios.h>
  221.   #include <stdio.h>
  222.  
  223.   /* baudrate settings are defined in <asm/termbits.h>, which is
  224.   included by <termios.h> */
  225.   #define BAUDRATE B38400
  226.   /* change this definition for the correct port */
  227.   #define MODEMDEVICE "/dev/ttyS1"
  228.   #define _POSIX_SOURCE 1 /* POSIX compliant source */
  229.  
  230.   #define FALSE 0
  231.   #define TRUE 1
  232.  
  233.   volatile int STOP=FALSE;
  234.  
  235.   main()
  236.   {
  237.     int fd,c, res;
  238.     struct termios oldtio,newtio;
  239.     char buf[255];
  240.   /*
  241.     Open modem device for reading and writing and not as controlling tty
  242.     because we don't want to get killed if linenoise sends CTRL-C.
  243.   */
  244.    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  245.    if (fd <0) {perror(MODEMDEVICE); exit(-1); }
  246.  
  247.    tcgetattr(fd,&oldtio); /* save current serial port settings */
  248.    bzero(newtio, sizeof(newtio)); /* clear the struct for new port settings */
  249.  
  250.   /*
  251.     BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
  252.     CRTSCTS : output hardware flow control (only used if the cable has
  253.               all necessary lines. See sect. 7 of Serial-HOWTO)
  254.     CS8     : 8n1 (8bit,no parity,1 stopbit)
  255.     CLOCAL  : local connection, no modem contol
  256.     CREAD   : enable receiving characters
  257.   */
  258.    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  259.  
  260.   /*
  261.     IGNPAR  : ignore bytes with parity errors
  262.     ICRNL   : map CR to NL (otherwise a CR input on the other computer
  263.               will not terminate input)
  264.     otherwise make device raw (no other input processing)
  265.   */
  266.    newtio.c_iflag = IGNPAR | ICRNL;
  267.  
  268.   /*
  269.    Raw output.
  270.   */
  271.    newtio.c_oflag = 0;
  272.  
  273.   /*
  274.     ICANON  : enable canonical input
  275.     disable all echo functionality, and don't send signals to calling program
  276.   */
  277.    newtio.c_lflag = ICANON;
  278.  
  279.   /*
  280.     initialize all control characters
  281.     default values can be found in /usr/include/termios.h, and are given
  282.     in the comments, but we don't need them here
  283.   */
  284.    newtio.c_cc[VINTR]    = 0;     /* Ctrl-c */
  285.    newtio.c_cc[VQUIT]    = 0;     /* Ctrl-\ */
  286.    newtio.c_cc[VERASE]   = 0;     /* del */
  287.    newtio.c_cc[VKILL]    = 0;     /* @ */
  288.    newtio.c_cc[VEOF]     = 4;     /* Ctrl-d */
  289.    newtio.c_cc[VTIME]    = 0;     /* inter-character timer unused */
  290.    newtio.c_cc[VMIN]     = 1;     /* blocking read until 1 character arrives */
  291.    newtio.c_cc[VSWTC]    = 0;     /* '\0' */
  292.    newtio.c_cc[VSTART]   = 0;     /* Ctrl-q */
  293.    newtio.c_cc[VSTOP]    = 0;     /* Ctrl-s */
  294.    newtio.c_cc[VSUSP]    = 0;     /* Ctrl-z */
  295.    newtio.c_cc[VEOL]     = 0;     /* '\0' */
  296.    newtio.c_cc[VREPRINT] = 0;     /* Ctrl-r */
  297.    newtio.c_cc[VDISCARD] = 0;     /* Ctrl-u */
  298.    newtio.c_cc[VWERASE]  = 0;     /* Ctrl-w */
  299.    newtio.c_cc[VLNEXT]   = 0;     /* Ctrl-v */
  300.    newtio.c_cc[VEOL2]    = 0;     /* '\0' */
  301.  
  302.   /*
  303.     now clean the modem line and activate the settings for the port
  304.   */
  305.    tcflush(fd, TCIFLUSH);
  306.    tcsetattr(fd,TCSANOW,&newtio);
  307.  
  308.   /*
  309.     terminal settings done, now handle input
  310.     In this example, inputting a 'z' at the beginning of a line will
  311.     exit the program.
  312.   */
  313.    while (STOP==FALSE) {     /* loop until we have a terminating condition */
  314.    /* read blocks program execution until a line terminating character is
  315.       input, even if more than 255 chars are input. If the number
  316.       of characters read is smaller than the number of chars available,
  317.       subsequent reads will return the remaining chars. res will be set
  318.       to the actual number of characters actually read */
  319.       res = read(fd,buf,255);
  320.       buf[res]=0;             /* set end of string, so we can printf */
  321.       printf(":%s:%d\n", buf, res);
  322.       if (buf[0]=='z') STOP=TRUE;
  323.    }
  324.    /* restore the old port settings */
  325.    tcsetattr(fd,TCSANOW,&oldtio);
  326.   }
  327.  
  328.   3.2.  Non-Canonical Input Processing
  329.  
  330.   In non-canonical input processing mode, input is not assembled into
  331.   lines and input processing (erase, kill, delete, etc.) does not occur.
  332.   Two parameters control the behaviour of this mode: c_cc[VTIME] sets
  333.   the character timer, and c_cc[VMIN] sets the minimum number of
  334.   characters to receive before satisfying the read.
  335.  
  336.   If MIN > 0 and TIME = 0, MIN sets the number of characters to receive
  337.   before the read is satisfied. As TIME is zero, the timer is not used.
  338.  
  339.   If MIN = 0 and TIME > 0, TIME serves as a timeout value. The read will
  340.   be satisfied if a single character is read, or TIME is exceeded (t =
  341.   TIME *0.1 s). If TIME is exceeded, no character will be returned.
  342.  
  343.   If MIN > 0 and TIME > 0, TIME serves as an inter-character timer. The
  344.   read will be satisfied if MIN characters are received, or the time
  345.   between two characters exceeds TIME. The timer is restarted every time
  346.   a character is received and only becomes active after the first
  347.   character has been received.
  348.  
  349.   If MIN = 0 and TIME = 0, read will be satisfied immedeately. The
  350.   number of characters currently available, or the number of characters
  351.   requested will be returned. According to Antonino (see contributions),
  352.   you could issue a fcntl(fd, F_SETFL, FNDELAY); before reading to get
  353.   the same result.
  354.  
  355.   By modifying newtio.c_cc[VTIME] and newtio.c_cc[VMIN] all modes
  356.   described above can be tested.
  357.  
  358.        #include <sys/types.h>
  359.        #include <sys/stat.h>
  360.        #include <fcntl.h>
  361.        #include <termios.h>
  362.        #include <stdio.h>
  363.  
  364.        #define BAUDRATE B38400
  365.        #define MODEMDEVICE "/dev/ttyS1"
  366.        #define _POSIX_SOURCE 1 /* POSIX compliant source */
  367.        #define FALSE 0
  368.        #define TRUE 1
  369.  
  370.        volatile int STOP=FALSE;
  371.  
  372.        main()
  373.        {
  374.          int fd,c, res;
  375.          struct termios oldtio,newtio;
  376.          char buf[255];
  377.  
  378.         fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  379.         if (fd <0) {perror(MODEMDEVICE); exit(-1); }
  380.  
  381.         tcgetattr(fd,&oldtio); /* save current port settings */
  382.  
  383.         bzero(newtio, sizeof(newtio));
  384.         newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  385.         newtio.c_iflag = IGNPAR;
  386.         newtio.c_oflag = 0;
  387.  
  388.         /* set input mode (non-canonical, no echo,...) */
  389.         newtio.c_lflag = 0;
  390.  
  391.         newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
  392.         newtio.c_cc[VMIN]     = 5;   /* blocking read until 5 chars received */
  393.  
  394.         tcflush(fd, TCIFLUSH);
  395.         tcsetattr(fd,TCSANOW,&newtio);
  396.  
  397.         while (STOP==FALSE) {       /* loop for input */
  398.           res = read(fd,buf,255);   /* returns after 5 chars have been input */
  399.           buf[res]=0;               /* so we can printf... */
  400.           printf(":%s:%d\n", buf, res);
  401.           if (buf[0]=='z') STOP=TRUE;
  402.         }
  403.         tcsetattr(fd,TCSANOW,&oldtio);
  404.        }
  405.  
  406.   3.3.  Asynchronous Input
  407.  
  408.   #include <termios.h>
  409.   #include <stdio.h>
  410.   #include <unistd.h>
  411.   #include <fcntl.h>
  412.   #include <sys/signal.h>
  413.   #include <sys/types.h>
  414.  
  415.   #define BAUDRATE B38400
  416.   #define MODEMDEVICE "/dev/ttyS1"
  417.   #define _POSIX_SOURCE 1 /* POSIX compliant source */
  418.   #define FALSE 0
  419.   #define TRUE 1
  420.  
  421.   volatile int STOP=FALSE;
  422.  
  423.   void signal_handler_IO (int status);   /* definition of signal handler */
  424.   int wait_flag=TRUE;                    /* TRUE while no signal received */
  425.  
  426.   main()
  427.   {
  428.     int fd,c, res;
  429.     struct termios oldtio,newtio;
  430.     struct sigaction saio;           /* definition of signal action */
  431.     char buf[255];
  432.  
  433.     /* open the device to be non-blocking (read will return immediatly) */
  434.     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
  435.     if (fd <0) {perror(MODEMDEVICE); exit(-1); }
  436.  
  437.     /* install the signal handler before making the device asynchronous */
  438.     saio.sa_handler = signal_handler_IO;
  439.     saio.sa_mask = 0;
  440.     saio.sa_flags = 0;
  441.     saio.sa_restorer = NULL;
  442.     sigaction(SIGIO,&saio,NULL);
  443.  
  444.     /* allow the process to receive SIGIO */
  445.     fcntl(fd, F_SETOWN, getpid());
  446.     /* Make the file descriptor asynchronous (the manual page says only
  447.        O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
  448.     fcntl(fd, F_SETFL, FASYNC);
  449.  
  450.     tcgetattr(fd,&oldtio); /* save current port settings */
  451.     /* set new port settings for canonical input processing */
  452.     newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  453.     newtio.c_iflag = IGNPAR | ICRNL;
  454.     newtio.c_oflag = 0;
  455.     newtio.c_lflag = ICANON;
  456.     newtio.c_cc[VMIN]=1;
  457.     newtio.c_cc[VTIME]=0;
  458.     tcflush(fd, TCIFLUSH);
  459.     tcsetattr(fd,TCSANOW,&newtio);
  460.  
  461.     /* loop while waiting for input. normally we would do something
  462.        useful here */
  463.     while (STOP==FALSE) {
  464.       printf(".\n");usleep(100000);
  465.       /* after receiving SIGIO, wait_flag = FALSE, input is available
  466.          and can be read */
  467.       if (wait_flag==FALSE) {
  468.         res = read(fd,buf,255);
  469.         buf[res]=0;
  470.         printf(":%s:%d\n", buf, res);
  471.         if (res==1) STOP=TRUE; /* stop loop if only a CR was input */
  472.         wait_flag = TRUE;      /* wait for new input */
  473.       }
  474.     }
  475.     /* restore old port settings */
  476.     tcsetattr(fd,TCSANOW,&oldtio);
  477.   }
  478.  
  479.   /***************************************************************************
  480.   * signal handler. sets wait_flag to FALSE, to indicate above loop that     *
  481.   * characters have been received.                                           *
  482.   ***************************************************************************/
  483.  
  484.   void signal_handler_IO (int status)
  485.   {
  486.     printf("received SIGIO signal.\n");
  487.     wait_flag = FALSE;
  488.   }
  489.  
  490.   3.4.  Waiting for Input from Multiple Sources
  491.  
  492.   This section is kept to a minimum. It is just intended to be a hint,
  493.   and therefore the example code is kept short. This will not only work
  494.   with serial ports, but with any set of file descriptors.
  495.  
  496.   The select call and acompanying macros use a fd_set. This is a bit
  497.   array, which has a bit entry for every valid file desciptor number.
  498.   select will accept a fd_set with the bits set for the relevant file
  499.   descriptors and returns a fd_set, in which the bits for the file
  500.   descriptors are set where input, output, or an exception occurred. All
  501.   handling of fd_set is done with the provided macros. See also the
  502.   manual page select(2).
  503.  
  504.   #include <sys/time.h>
  505.   #include <sys/types.h>
  506.   #include <unistd.h>
  507.  
  508.   main()
  509.   {
  510.      int    fd1, fd2;  /* input sources 1 and 2 */
  511.      fd_set readfs;    /* file descriptor set */
  512.      int    maxfd;     /* mixmum file desciptor used */
  513.      int    loop=1;    /* loop while TRUE */
  514.  
  515.      /* open_input_source opens a device, sets the port correctly, and
  516.         returns a file descriptor */
  517.      fd1 = open_input_source("/dev/ttyS1");   /* COM2 */
  518.      if (fd1<0) exit(0);
  519.      fd2 = open_input_source("/dev/ttyS2");   /* COM3 */
  520.      if (fd2<0) exit(0);
  521.      maxfd = MAX (fd1, fd2)+1;  /* maximum bit entry (fd) to test */
  522.  
  523.      /* loop for input */
  524.      while (loop) {
  525.        FD_SET(fd1, &readfs);  /* set testing for source 1 */
  526.        FD_SET(fd2, &readfs);  /* set testing for source 2 */
  527.        /* block until input becomes available */
  528.        select(maxfd, &readfs, NULL, NULL, NULL);
  529.        if (FD_ISSET(fd1))         /* input from source 1 available */
  530.          handle_input_from_source1();
  531.        if (FD_ISSET(fd2))         /* input from source 2 available */
  532.          handle_input_from_source2();
  533.      }
  534.  
  535.   }
  536.  
  537.   The given example blocks indefinitely, until input from one of the
  538.   sources becomes available. If you need to timeout on input, just
  539.   replace the select call by:
  540.  
  541.        int res;
  542.        struct timeval Timeout;
  543.  
  544.        /* set timeout value within input loop */
  545.        Timeout.tv_usec = 0;  /* milliseconds */
  546.        Timeout.tv_sec  = 1;  /* seconds */
  547.        res = select(maxfd, &readfs, NULL, NULL, &Timeout);
  548.        if (res==0)
  549.        /* number of filedescriptors with input = 0, timeout occurred. */
  550.  
  551.   This example will timeout after 1 second. If a timeout occurs, select
  552.   will return 0, but beware that Timeout is decremented by the time
  553.   actually waited for input by select. If the timeout value is zero,
  554.   select will return immedeatly.
  555.  
  556.   4.  Other Sources of Information
  557.  
  558.   ╖  The Linux Serial-HOWTO describes how to set up serial ports and
  559.      contains hardware information.
  560.  
  561.   ╖  Serial Programming Guide for POSIX Compliant Operating Systems
  562.      <http://www.easysw.com/~mike/serial>, by Michael Sweet.
  563.  
  564.   ╖  The manual page termios(3) describes all flags for the termios
  565.      structure.
  566.  
  567.   5.  Contributions
  568.  
  569.   As mentioned in the introduction, I am no expert in this field, but
  570.   had problems myself, and found a solution with the help of others.
  571.   Thanks for the help from Mr. Strudthoff from the European Transonic
  572.   Windtunnel, Cologne, Michael Carter (mcarter@rocke.electro.swri.edu,
  573.   and Peter Waltenberg (p.waltenberg@karaka.chch.cri.nz)
  574.  
  575.   Antonino Ianella (antonino@usa.net wrote the Serial-Port-Programming
  576.   Mini HOWTO, at the same time I prepared this document. Greg Hankins
  577.   asked me to incorporate Antonino's Mini-HOWTO into this document.
  578.  
  579.   The structure of this document and SGML formatting was derived from
  580.   the Serial-HOWTO by Greg Hankins.
  581.  
  582.